home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2015 August / PC_Format_082015.iso / Pełne wersje / F-Secure FREEDOME VPN 1.0.11 / Freedome.exe / QtGraphicalEffects / private / FastInnerShadow.qml < prev    next >
Text File  |  2015-04-13  |  14KB  |  414 lines

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. **   * Redistributions of source code must retain the above copyright
  15. **     notice, this list of conditions and the following disclaimer.
  16. **   * Redistributions in binary form must reproduce the above copyright
  17. **     notice, this list of conditions and the following disclaimer in
  18. **     the documentation and/or other materials provided with the
  19. **     distribution.
  20. **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. **     of its contributors may be used to endorse or promote products derived
  22. **     from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40.  
  41. import QtQuick 2.0
  42.  
  43. Item {
  44.     id: rootItem
  45.     property variant source
  46.     property real blur: 0.0
  47.     property real horizontalOffset: 0
  48.     property real verticalOffset: 0
  49.     property real spread: 0.0
  50.     property color color: "black"
  51.     property bool cached: false
  52.  
  53.     SourceProxy {
  54.         id: sourceProxy
  55.         input: rootItem.source
  56.     }
  57.  
  58.     ShaderEffectSource {
  59.         id: cacheItem
  60.         anchors.fill: shaderItem
  61.         visible: rootItem.cached
  62.         smooth: true
  63.         sourceItem: shaderItem
  64.         live: true
  65.         hideSource: visible
  66.     }
  67.  
  68.     property string __internalBlurVertexShader: "
  69.         attribute highp vec4 qt_Vertex;
  70.         attribute highp vec2 qt_MultiTexCoord0;
  71.         uniform highp mat4 qt_Matrix;
  72.         uniform highp float yStep;
  73.         uniform highp float xStep;
  74.         varying highp vec2 qt_TexCoord0;
  75.         varying highp vec2 qt_TexCoord1;
  76.         varying highp vec2 qt_TexCoord2;
  77.         varying highp vec2 qt_TexCoord3;
  78.  
  79.         void main() {
  80.             qt_TexCoord0 = vec2(qt_MultiTexCoord0.x + xStep, qt_MultiTexCoord0.y + yStep * 0.36);
  81.             qt_TexCoord1 = vec2(qt_MultiTexCoord0.x + xStep * 0.36, qt_MultiTexCoord0.y - yStep);
  82.             qt_TexCoord2 = vec2(qt_MultiTexCoord0.x - xStep * 0.36, qt_MultiTexCoord0.y + yStep);
  83.             qt_TexCoord3 = vec2(qt_MultiTexCoord0.x - xStep, qt_MultiTexCoord0.y - yStep * 0.36);
  84.             gl_Position = qt_Matrix * qt_Vertex;
  85.         }
  86.     "
  87.  
  88.     property string __internalBlurFragmentShader: "
  89.         uniform lowp sampler2D source;
  90.         uniform lowp float qt_Opacity;
  91.         varying highp vec2 qt_TexCoord0;
  92.         varying highp vec2 qt_TexCoord1;
  93.         varying highp vec2 qt_TexCoord2;
  94.         varying highp vec2 qt_TexCoord3;
  95.  
  96.         void main() {
  97.             highp vec4 sourceColor = (texture2D(source, qt_TexCoord0) +
  98.             texture2D(source, qt_TexCoord1) +
  99.             texture2D(source, qt_TexCoord2) +
  100.             texture2D(source, qt_TexCoord3)) * 0.25;
  101.             gl_FragColor = sourceColor * qt_Opacity;
  102.         }
  103.    "
  104.  
  105.     ShaderEffect {
  106.         id: level0
  107.         property variant source: sourceProxy.output
  108.         property real horizontalOffset: rootItem.horizontalOffset / rootItem.width
  109.         property real verticalOffset: rootItem.verticalOffset / rootItem.width
  110.         property color color: rootItem.color
  111.  
  112.         anchors.fill: parent
  113.         visible: false
  114.         smooth: true
  115.         fragmentShader: "
  116.             varying highp vec2 qt_TexCoord0;
  117.             uniform lowp float qt_Opacity;
  118.             uniform highp sampler2D source;
  119.             uniform lowp vec4 color;
  120.             uniform highp float horizontalOffset;
  121.             uniform highp float verticalOffset;
  122.  
  123.             void main(void) {
  124.                 highp vec2 pos = qt_TexCoord0 - vec2(horizontalOffset, verticalOffset);
  125.                 lowp float ea = step(0.0, pos.x) * step(0.0, pos.y) * step(pos.x, 1.0) * step(pos.y, 1.0);
  126.                 lowp float eb = 1.0 - ea;
  127.                 gl_FragColor = (eb * color + ea * color * (1.0 - texture2D(source, pos).a)) * qt_Opacity;
  128.             }
  129.         "
  130.     }
  131.  
  132.     ShaderEffectSource {
  133.         id: level1
  134.         width: Math.ceil(shaderItem.width / 32) * 32
  135.         height: Math.ceil(shaderItem.height / 32) * 32
  136.         sourceItem: level0
  137.         hideSource: rootItem.visible
  138.         smooth: true
  139.         visible: false
  140.     }
  141.  
  142.     ShaderEffect {
  143.         id: effect1
  144.         property variant source: level1
  145.         property real yStep: 1/height
  146.         property real xStep: 1/width
  147.         anchors.fill: level2
  148.         visible: false
  149.         smooth: true
  150.         vertexShader: __internalBlurVertexShader
  151.         fragmentShader: __internalBlurFragmentShader
  152.     }
  153.  
  154.     ShaderEffectSource {
  155.         id: level2
  156.         width: level1.width / 2
  157.         height: level1.height / 2
  158.         sourceItem: effect1
  159.         hideSource: rootItem.visible
  160.         visible: false
  161.         smooth: true
  162.     }
  163.  
  164.     ShaderEffect {
  165.         id: effect2
  166.         property variant source: level2
  167.         property real yStep: 1/height
  168.         property real xStep: 1/width
  169.         anchors.fill: level3
  170.         visible: false
  171.         smooth: true
  172.         vertexShader: __internalBlurVertexShader
  173.         fragmentShader: __internalBlurFragmentShader
  174.     }
  175.  
  176.     ShaderEffectSource {
  177.         id: level3
  178.         width: level2.width / 2
  179.         height: level2.height / 2
  180.         sourceItem: effect2
  181.         hideSource: rootItem.visible
  182.         visible: false
  183.         smooth: true
  184.     }
  185.  
  186.     ShaderEffect {
  187.         id: effect3
  188.         property variant source: level3
  189.         property real yStep: 1/height
  190.         property real xStep: 1/width
  191.         anchors.fill: level4
  192.         visible: false
  193.         smooth: true
  194.         vertexShader: __internalBlurVertexShader
  195.         fragmentShader: __internalBlurFragmentShader
  196.     }
  197.  
  198.     ShaderEffectSource {
  199.         id: level4
  200.         width: level3.width / 2
  201.         height: level3.height / 2
  202.         sourceItem: effect3
  203.         hideSource: rootItem.visible
  204.         visible: false
  205.         smooth: true
  206.     }
  207.  
  208.     ShaderEffect {
  209.         id: effect4
  210.         property variant source: level4
  211.         property real yStep: 1/height
  212.         property real xStep: 1/width
  213.         anchors.fill: level5
  214.         visible: false
  215.         smooth: true
  216.         vertexShader: __internalBlurVertexShader
  217.         fragmentShader: __internalBlurFragmentShader
  218.     }
  219.  
  220.     ShaderEffectSource {
  221.         id: level5
  222.         width: level4.width / 2
  223.         height: level4.height / 2
  224.         sourceItem: effect4
  225.         hideSource: rootItem.visible
  226.         visible: false
  227.         smooth: true
  228.     }
  229.  
  230.     ShaderEffect {
  231.         id: effect5
  232.         property variant source: level5
  233.         property real yStep: 1/height
  234.         property real xStep: 1/width
  235.         anchors.fill: level6
  236.         visible: false
  237.         smooth: true
  238.         vertexShader: __internalBlurVertexShader
  239.         fragmentShader: __internalBlurFragmentShader
  240.     }
  241.  
  242.     ShaderEffectSource {
  243.         id: level6
  244.         width: level5.width / 2
  245.         height: level5.height / 2
  246.         sourceItem: effect5
  247.         hideSource: rootItem.visible
  248.         visible: false
  249.         smooth: true
  250.     }
  251.  
  252.     Item {
  253.         id: dummysource
  254.         width: 1
  255.         height: 1
  256.         visible: false
  257.     }
  258.  
  259.     ShaderEffectSource {
  260.         id: dummy
  261.         width: 1
  262.         height: 1
  263.         sourceItem: dummysource
  264.         visible: false
  265.         smooth: false
  266.         live: false
  267.     }
  268.  
  269.     ShaderEffect {
  270.         id: shaderItem
  271.         width: parent.width
  272.         height: parent.height
  273.  
  274.         property variant original: sourceProxy.output
  275.         property variant source1: level1
  276.         property variant source2: level2
  277.         property variant source3: level3
  278.         property variant source4: level4
  279.         property variant source5: level5
  280.         property variant source6: level6
  281.         property real lod: rootItem.blur
  282.  
  283.         property real weight1;
  284.         property real weight2;
  285.         property real weight3;
  286.         property real weight4;
  287.         property real weight5;
  288.         property real weight6;
  289.  
  290.         property real spread: 1.0 - (rootItem.spread * 0.98)
  291.         property color color: rootItem.color
  292.  
  293.         function weight(v) {
  294.             if (v <= 0.0)
  295.                 return 1
  296.             if (v >= 0.5)
  297.                 return 0
  298.  
  299.             return 1.0 - v / 0.5
  300.         }
  301.  
  302.         function calculateWeights() {
  303.  
  304.             var w1 = weight(Math.abs(lod - 0.100))
  305.             var w2 = weight(Math.abs(lod - 0.300))
  306.             var w3 = weight(Math.abs(lod - 0.500))
  307.             var w4 = weight(Math.abs(lod - 0.700))
  308.             var w5 = weight(Math.abs(lod - 0.900))
  309.             var w6 = weight(Math.abs(lod - 1.100))
  310.  
  311.             var sum = w1 + w2 + w3 + w4 + w5 + w6;
  312.             weight1 = w1 / sum;
  313.             weight2 = w2 / sum;
  314.             weight3 = w3 / sum;
  315.             weight4 = w4 / sum;
  316.             weight5 = w5 / sum;
  317.             weight6 = w6 / sum;
  318.  
  319.             upateSources()
  320.         }
  321.  
  322.         function upateSources() {
  323.             var sources = new Array();
  324.             var weights = new Array();
  325.  
  326.             if (weight1 > 0) {
  327.                 sources.push(level1)
  328.                 weights.push(weight1)
  329.             }
  330.  
  331.             if (weight2 > 0) {
  332.                 sources.push(level2)
  333.                 weights.push(weight2)
  334.             }
  335.  
  336.             if (weight3 > 0) {
  337.                 sources.push(level3)
  338.                 weights.push(weight3)
  339.             }
  340.  
  341.             if (weight4 > 0) {
  342.                 sources.push(level4)
  343.                 weights.push(weight4)
  344.             }
  345.  
  346.             if (weight5 > 0) {
  347.                 sources.push(level5)
  348.                 weights.push(weight5)
  349.             }
  350.  
  351.             if (weight6 > 0) {
  352.                 sources.push(level6)
  353.                 weights.push(weight6)
  354.             }
  355.  
  356.             for (var j = sources.length; j < 6; j++) {
  357.                 sources.push(dummy)
  358.                 weights.push(0.0)
  359.             }
  360.  
  361.             source1 = sources[0]
  362.             source2 = sources[1]
  363.             source3 = sources[2]
  364.             source4 = sources[3]
  365.             source5 = sources[4]
  366.             source6 = sources[5]
  367.  
  368.             weight1 = weights[0]
  369.             weight2 = weights[1]
  370.             weight3 = weights[2]
  371.             weight4 = weights[3]
  372.             weight5 = weights[4]
  373.             weight6 = weights[5]
  374.         }
  375.  
  376.         Component.onCompleted: calculateWeights()
  377.  
  378.         onLodChanged: calculateWeights()
  379.  
  380.         fragmentShader: "
  381.             uniform lowp sampler2D original;
  382.             uniform lowp sampler2D source1;
  383.             uniform lowp sampler2D source2;
  384.             uniform lowp sampler2D source3;
  385.             uniform lowp sampler2D source4;
  386.             uniform lowp sampler2D source5;
  387.             uniform mediump float weight1;
  388.             uniform mediump float weight2;
  389.             uniform mediump float weight3;
  390.             uniform mediump float weight4;
  391.             uniform mediump float weight5;
  392.             uniform highp vec4 color;
  393.             uniform highp float spread;
  394.             uniform lowp float qt_Opacity;
  395.             varying mediump vec2 qt_TexCoord0;
  396.  
  397.             highp float linearstep(highp float e0, highp float e1, highp float x) {
  398.                 return clamp((x - e0) / (e1 - e0), 0.0, 1.0);
  399.             }
  400.  
  401.             void main() {
  402.                 lowp vec4 shadowColor = texture2D(source1, qt_TexCoord0) * weight1;
  403.                 shadowColor += texture2D(source2, qt_TexCoord0) * weight2;
  404.                 shadowColor += texture2D(source3, qt_TexCoord0) * weight3;
  405.                 shadowColor += texture2D(source4, qt_TexCoord0) * weight4;
  406.                 shadowColor += texture2D(source5, qt_TexCoord0) * weight5;
  407.                 lowp vec4 originalColor = texture2D(original, qt_TexCoord0);
  408.                 shadowColor.rgb = mix(originalColor.rgb, color.rgb * originalColor.a, linearstep(0.0, spread, shadowColor.a));
  409.                 gl_FragColor = vec4(shadowColor.rgb, originalColor.a) * originalColor.a * qt_Opacity;
  410.             }
  411.         "
  412.     }
  413. }
  414.